//
//  XNicknameAlertView.m
//  ReceiveVouchers
//
//  Created by Veer on 2017/11/15.
//  Copyright © 2017年 ReceiveVouchers. All rights reserved.
//

#import "XNicknameAlertView.h"

@interface XNicknameAlertView() <UITextFieldDelegate>

@property (nonatomic, weak) UILabel *tipLbl;

@end

@implementation XNicknameAlertView

- (instancetype)init
{

    if (self = [super initWithTitle:@"编辑备注" sureBtnTitle:@"确定" cancleBtnTitle:@"取消" size:CGSizeMake(280, 200)]) {
        
        WeakSelf(weakSelf)
        self.isHidenAlertBySure = NO;
        self.ActionBlock = ^(NSInteger index) {
            
            if (index == 1) {
                if (weakSelf.SaveBlock) {
                    weakSelf.SaveBlock(weakSelf.textView.text);
                }
            }
        };
        [self setupSubView];
        
        //监听当键盘将要出现时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        //监听当键将要退出时
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    return self;
}

- (void)setupSubView
{
    
//    self.sureBtn.enabled = NO;
    
    UITextField *textView = [[UITextField alloc] init];
    [textView becomeFirstResponder];
    textView.delegate = self;
    textView.backgroundColor = RGB(244, 244, 244);
    textView.textColor = RGB(34, 34, 34);
    textView.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    textView.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    textView.leftViewMode = UITextFieldViewModeAlways;
    textView.placeholder = @"最长为6个字";
    textView.layer.cornerRadius = 2;
    textView.layer.borderWidth = 0.5;
    textView.layer.borderColor = RGB(244, 244, 244).CGColor;
    [textView addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.alertView addSubview:textView];
    self.textView = textView;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(23);
        make.top.mas_equalTo(80);
        make.right.mas_equalTo(-23);
        make.height.mas_equalTo(44);
    }];

    UILabel *tipLbl = [[UILabel alloc] init];
    tipLbl.hidden = YES;
    tipLbl.text = @"该昵称已被注册，请重新输入";
    tipLbl.textColor = RGB(249, 78, 84);
    tipLbl.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    [self.alertView addSubview:tipLbl];
    self.tipLbl = tipLbl;
    [tipLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(textView);
        make.top.mas_equalTo(textView.mas_bottom).offset(10);
        make.height.mas_equalTo(12);
    }];
    
    UITapGestureRecognizer *tapGeture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGesture:)];
    [self addGestureRecognizer:tapGeture];
    
    UITapGestureRecognizer *tapGeture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGesture2:)];
    [self.alertView addGestureRecognizer:tapGeture2];
}

- (void)showTipWith:(NSString *)message;
{
    self.tipLbl.hidden = NO;
    self.tipLbl.text = message;
    self.textView.layer.borderColor = RGB(249, 78, 84).CGColor;
    self.textView.backgroundColor = RGB(255, 238, 238);
}

- (void)hideTip
{
    self.tipLbl.hidden = YES;
    self.textView.layer.borderColor = RGB(244, 244, 244).CGColor;
    self.textView.backgroundColor = RGB(244, 244, 244);
}

- (void)show
{
    [super show];
    [self.textView becomeFirstResponder];
}

#pragma mark - action
- (void)onTapGesture:(UITapGestureRecognizer *)tapGeture
{
    [self endEditing:YES];
}

- (void)onTapGesture2:(UITapGestureRecognizer *)tapGeture
{
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [self endEditing:YES];
//}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    if (string.length > 0 && textField.text.length >= 20) {
        
        return NO;
    }
    return YES;
}

- (void)textFieldDidChanged:(UITextField *)textField
{
//    self.sureBtn.enabled = textField.text.length > 0;
}


//当键盘出现
- (void)keyboardWillShow:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGRect keyboardRect = [value CGRectValue];
    CGFloat height = keyboardRect.size.height;
    
    CGFloat bottomMargin = kScreenHeight - CGRectGetMaxY(self.alertView.frame);
    
    [UIView animateWithDuration:duration animations:^{
        if (kScreenWidth < 375) {
            self.y = -70;
        }else{
            self.y = -50;
        }
    }];
}

//当键退出
- (void)keyboardWillHide:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    CGFloat height = keyboardRect.size.height;
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    [UIView animateWithDuration:duration animations:^{
        self.y = 0;
    }];
    
}


#pragma mark - setter && getter
- (void)setText:(NSString *)text
{
    _text = text;
    
    self.textView.text = text;
}


@end
